package com.woyowodaows.popupwind;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.woyowodaows.R;
import com.woyowodaows.activity.MainActivity;
import com.woyowodaows.activity.home.LoginActivity;
import com.woyowodaows.activity.home.NewNoticeDetailsActivity;
import com.woyowodaows.activity.mine.OrderInformationActivity;
import com.woyowodaows.bean.home.JpushBean;
import com.woyowodaows.bean.home.NoticeBean;
import com.woyowodaows.utils.SPUtils;


import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

import static android.content.Context.NOTIFICATION_SERVICE;
import static android.content.Context.VIBRATOR_SERVICE;
import static com.woyowodaows.application.MyApplication.getApplicationContext;

/**
 * Created by Administrator on 2019/3/7.
 */

public class JPushReceiver extends BroadcastReceiver {
    private int targetId;
    private String targetType;
    private  String object;
    private String  filePath;
    private   Ringtone r;
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            //官网提供根据Registration ID 进行推送 此方法用于处理该类推送消息
        }else {
            if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                // 自定义消息不会展示在通知栏，完全要开发者写代码去处理
                // 处理自定义消息
                Log.i("getAction",intent.getAction());
            } else {
                if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                    //接收到通知会走的方法
                    Log.i("getAction",intent.getAction());
                    // processCustomMessage(context,bundle);
                    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                    r.play();
                    Vibrator vibrator = (Vibrator) context.getSystemService(VIBRATOR_SERVICE);
                    vibrator.vibrate(1000);
                    if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                        Log.i("getAction",intent.getAction());
                        r.stop();
                        //用户点击通知会走的方法
//                        会返回这两个扩展数据
//                        slug 数据id  订单id或消息id
//                        type 跳转类型：message消息详细
//                        repair_detail订单详细
//                        home首页
                        //获取推送消息的方法
                        String bean = bundle.getString(JPushInterface.EXTRA_EXTRA);
                        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
                        Gson gson = new Gson();
                        JpushBean info = gson.fromJson(bean, JpushBean.class);
                        // 在这里可以自己写代码去定义用户点击后的行为
                        if (bean!= null && info!= null) {
                            if(info.getType()!=null){
                                targetType=info.getType();
                            }else{
                                targetType="无";
                            }
                            if (targetType.equals("home")) {/// home=>首页
                                Intent i = new Intent(context, MainActivity.class); //打开主界面
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(i);
                            }else if((targetType.equals("message")) ){// message> 消息
                                Intent intentNewNot=new Intent(context, NewNoticeDetailsActivity.class);
                                NoticeBean bt=new NoticeBean();
                                bt.setType("notice");
                                bt.setTitle(title);
                                bt.setSlug(info.getSlug());
                                bt.setCommunity("message");
                                Bundle bundles=new Bundle();
                                bundles.putSerializable("news",bt);
                                intentNewNot.putExtras(bundles);
                                context.startActivity(intentNewNot);
                            }else if((targetType.equals("repair_detail")) ){ // 订单详情
                                boolean isLogin = (boolean) SPUtils.get(context, SPUtils.IS_LOGIN, false);
                                if (!isLogin) {
                                    context.startActivity(new Intent(context, LoginActivity.class));
                                }else {
                                    Intent intentOrder=new Intent(context,OrderInformationActivity.class);
                                    intentOrder.putExtra("OrderSn",info.getSlug()+"");
                                    context.startActivity(intentOrder);
                                }
                            }else {
                                Intent i = new Intent(context, MainActivity.class); //打开主界面
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(i);
                            }
                        } else {
                            Log.d("-------", "null");
                        }
                    } else {
                    }
                }
            }
        }
    }

}
